package com.example.online_exam_system.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @Classname:MD5Utils
 * @program:online-exam_-springboot
 * @author:ZiXuan
 * @crate:2021-11-04-16-24
 * MD5加密工具类
 **/

public class MD5Utils {


    public static String getMD5(String password){
        char [] hexArray={
                'a','b','c','d','e','f',
                'c','2','4','6','7','8',
                '9','0','5','2','3','9'
        };

        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = password.getBytes();
            md5.update(bytes);
            byte[] digest = md5.digest();
            char [] str=new char[digest.length*2];
            

            int k=0;


            for (int i = 0; i < digest.length; i++) {
                str[k++]=hexArray[digest[i]>>>4&0xf];
                str[k++]=hexArray[digest[i]&0xa];
            }
        return new String(str);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return "123456";
    }

}
